/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdbcdaos;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;
import jdbcpojos.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

/**
 * @todo zrobic zapis uzytkownika
 * @author piotrek
 */
@Repository
public class UserDao {

	private JdbcTemplate jdbcTemplate;

	public UserDao(){
		
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {

		this.jdbcTemplate = new JdbcTemplate(dataSource);

	}

	/**
	 * Aktualizacja danych klienta przez Jdbc
	 * @return boolean
	 */
	public boolean updateUser( Customer c ){

		this.jdbcTemplate.update("UPDATE customer SET login = ? WHERE id_customer = ?",
				new Object[]{c.getLogin(), c.getId_customer()});
		this.jdbcTemplate.update("UPDATE customer_data SET street = ? WHERE id_customer = ?",
				new Object[]{c.getStreet(), c.getId_customer()});

		return true;

	}

	/**
	 * Usuwanie danych uzytkownika
	 * @return
	 */
	public boolean deleteUser( int id ){

		this.jdbcTemplate.update( "DELETE FROM purchase WHERE id_customer = ?", new Object[]{id} );
		this.jdbcTemplate.update( "DELETE FROM customer_data WHERE id_customer = ?", new Object[]{id} );
		this.jdbcTemplate.update( "DELETE FROM customer WHERE id_customer = ?", new Object[]{id} );
		
		return true;

	}

	/**
	 * Dodawanie nowego klienta przez Jdbc
	 * @return
	 */
	public boolean addUser( Customer c ){

		//czy nie trzeba wywolac sekwencji i pobrac klucza? lub zastosowac keyHolder
		int id = this.jdbcTemplate.queryForInt("SELECT customer_seq.nextval from dual");
		this.jdbcTemplate.update("INSERT INTO customer( id_customer, login, passwd ) VALUES( ?, ?, ? )",
				new Object[]{ id, c.getLogin(), c.getPasswd()});

		this.jdbcTemplate.update("INSERT INTO customer_data( id_customer_data ,city, flat_number, name, postal, street, street_number, surname, id_Customer ) VALUES( customer_data_seq.nextval, ?, ?, ?, ?, ?, ?, ?, ? )",
				new Object[]{c.getCity(), c.getFlat_number(), c.getName(), c.getPostal(), c.getStreet(), c.getStreet_number(),c.getSurname(), id});

		return true;
		
	}

	/**
	 * Pobieranie danych uzytkownika
	 * @param id
	 * @return
	 * @throws SQLException
	 * @throws NamingException
	 */
	public Customer getUser( int id ) throws SQLException, NamingException{

		Customer customer;

		/**
		 * Wersja z jdbcTemplate
		 */
		customer = this.jdbcTemplate.queryForObject(
					"SELECT c.id_customer, c.login, c.passwd, cd.name, cd.surname, cd.city, cd.street, cd.postal, cd.street_number, cd.flat_number FROM customer c, customer_data cd WHERE c.id_customer = ? and c.id_customer = cd.id_customer",
					new Object[]{ id },
					new RowMapper<Customer>() {

							public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
							Customer c = new Customer();
							c.setId_customer(rs.getLong("id_customer"));
							c.setLogin(rs.getString("login"));
							c.setPasswd(rs.getString("passwd"));
							c.setName(rs.getString("name"));
							c.setSurname(rs.getString("surname"));
							c.setCity(rs.getString("city"));
							c.setStreet(rs.getString("street"));
							c.setPostal(rs.getString("postal"));
							c.setStreet_number(rs.getInt("street_number"));
							c.setFlat_number(rs.getInt("flat_number"));
							return c;
						}

						}

					);

		return customer;

	}


}
